<?php
/**
 * ×(Deprecated) eAccelerator 缓存操作, 默认配置于 php.ini 中; 
 *  使用之前, 确保编译时加参数 --with-eaccelerator-shared-memory
 *
 * @filesource eaccelerator.class.php
 * @package peck
 * @subpackage _cache
 * @version $id: 0.1, utf8, Mon Dec 14 22:11:28 CST 2009
 * @author LD King <kldscs[at]gmail.com>
 * @copyright Copyleft (D.) 2007 - 2009 MiFunny China Inc.
 * @link http://mifunny.info/
 * @see datafile.class.php
 *   http://bart.eaccelerator.net/doc/phpdoc/
 *   http://lovely.blog.51cto.com/1045933/235912 
 */
class eaccelerator{ //implements _DsCache{
    
	/**
	 * 默认的缓存策略
	 *
	 * DSCACHE_LIFE_TIME - life_time: 缓存有效时间(秒), 默认值 900
     *    如果设置为 0 表示缓存总是失效,
     *    设置为 -1 则表示不检查缓存有效期.
     *
	 * @var int
	 */
	private $life_time = 900;
    
    public function __construct(array $policy=null){
        $this->_init( $policy );
    }
    public function _init(array $policy=null){
        //if( !function_exists('eaccelerator_list_keys') )
        //    throw new LogicException("Error: eAccelerator Shared Menmory feature...");
        if( !is_null($policy['life_time']) ) 
            $this->life_time = $policy['life_time'];
    }

    public function set($id, $data, array $policy=null){
        $ttl = $this->_policy('eaccelerator_put', $policy);
        if( empty($ttl) ) return false;
        if( $ttl<0 ) $ttl = 0;

        eaccelerator_lock($id);
        eaccelerator_put($id, $data, $ttl);
        eaccelerator_unlock($id);
        return true;
    } 

    public function get($id, array $policy=null){
        $this->_policy('eaccelerator_get', $policy);
        return eaccelerator_get($id);
    }

    public function remove($id, array $policy=null){
        $this->_policy('eaccelerator_rm', $policy);
        return eaccelerator_rm($id); 
    }

    /**
     *  清空缓存
     */
    public function clean(array $policy=null){
        $this->_policy('eaccelerator_gc', $policy);
        eaccelerator_gc();
    }

    protected function _policy($func, array $policy=null){
        if( function_exists($func) ){
            if( !is_null($policy['life_time']) ) 
                $ttl = $policy['life_time'];
            else $ttl = $this->life_time;
            
            return $ttl;
        }else throw LogicException("Error: No eAccelerator Cache Function: {$func} !");
    }


    //    ---->  未涉及到的函数
    //eaccelerator_cache_output($key, $eval_code, $ttl=0)
    //  将 $eval_code 代码的输出缓存 $ttl 秒，（$ttl参数同 eacclerator_put）
    //  例如:  eaccelerator_cache_output('test', 'echo time(); phpinfo();', 30);
    //       
    //eaccelerator_cache_result($key, $eval_code, $ttl=0)
    //  将 $eval_code 代码的执行结果缓存 $ttl 秒，（$ttl参数同 eacclerator_put），类似 cache_output
    //  例如:  eaccelerator_cache_result('test', ' time() . "Hello";', 30);
    // 
    //eaccelerator_cache_page($key, $ttl=0)
    //  将当前整页缓存 $ttl 秒。
    //  例如:  eaccelerator_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET),30);
    //         echo time();  phpinfo();
    //                                   
    //eaccelerator_rm_page($key)
    //  删除由  eaccelerator_cache_page() 执行的缓存，参数也是 $key
}//END class eaccelerator
?>
